﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BYManageInfoSystem
{
    public partial class Frm_BYFeeKaiPiaoCateBankCount : Form
    {
        private DGBY.BLL.BYFeeKaiPiaoCateTab m_bllBYFeeKaiPiaoCateTab = new DGBY.BLL.BYFeeKaiPiaoCateTab();
        private DGBY.BLL.BYFeeKaiPiaoCateBankCountTab m_bllBYFeeKaiPiaoCateBankCountTab = new DGBY.BLL.BYFeeKaiPiaoCateBankCountTab();
        private DataSet m_ds = new DataSet();
        private DataSet m_AccountDs = new DataSet();
        int m_nSelectBYFeeKaiPiaoCateTabID = 0;
        int m_nSelectBYFeeKaiPiaoCateBankCountTabID = 0;

        public Frm_BYFeeKaiPiaoCateBankCount()
        {
            InitializeComponent();
        }

        private void ShowAllRecordInDataGridView()
        {
            dataGridViewCompList.DataSource = null;
            m_ds = m_bllBYFeeKaiPiaoCateTab.GetAllList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewCompList.DataSource = m_ds.Tables[0];
                dataGridViewCompList.Columns[0].Visible = false;
            }

        }

        private void buttonShowAllRecord_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.BYFeeKaiPiaoCateTab modelBYFeeKaiPiaoCateTab = new DGBY.Model.BYFeeKaiPiaoCateTab();
                modelBYFeeKaiPiaoCateTab.BYFeeKaiPiaoCateName = this.textBoxCompName.Text;


                int nResult = m_bllBYFeeKaiPiaoCateTab.Add(modelBYFeeKaiPiaoCateTab);


                m_nSelectBYFeeKaiPiaoCateTabID = m_bllBYFeeKaiPiaoCateTab.GetMaxId() - 1;
                ShowAllRecordInDataGridView();
                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                InitSelfCompCombox();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
       
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.BYFeeKaiPiaoCateTab modelBYFeeKaiPiaoCateTab = m_bllBYFeeKaiPiaoCateTab.GetModel(m_nSelectBYFeeKaiPiaoCateTabID);
                if (modelBYFeeKaiPiaoCateTab == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                modelBYFeeKaiPiaoCateTab.BYFeeKaiPiaoCateName = this.textBoxCompName.Text;

                m_bllBYFeeKaiPiaoCateTab.Update(modelBYFeeKaiPiaoCateTab);

                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (m_bllBYFeeKaiPiaoCateTab.GetModel(m_nSelectBYFeeKaiPiaoCateTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllBYFeeKaiPiaoCateTab.Delete(m_nSelectBYFeeKaiPiaoCateTabID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            
            InitSelfCompCombox();
            ShowAllAccountRecordInDataGridView();
        }

        private void InitSelfCompCombox()
        {
            DataSet ds = m_bllBYFeeKaiPiaoCateTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxCompName.DataSource = ds.Tables[0];
                this.comboBoxCompName.DisplayMember = "开票明细分类名";
                this.comboBoxCompName.ValueMember = "BYFeeKaiPiaoCateTabID";
                this.comboBoxCompName.SelectedIndex = 0;
            }
        }

        private void Frm_BYFeeKaiPiaoCateBankCount_Load(object sender, EventArgs e)
        {
            InitSelfCompCombox();
        }

        private void ShowAllAccountRecordInDataGridView()
        {
            dataGridViewCompBankList.DataSource = null;
            m_AccountDs = m_bllBYFeeKaiPiaoCateBankCountTab.GetAllAccountInfo("");
            if (m_AccountDs.Tables.Count > 0 && m_AccountDs.Tables[0].Rows.Count > 0)
            {
                dataGridViewCompBankList.DataSource = m_AccountDs.Tables[0];
                dataGridViewCompBankList.Columns[0].Visible = false;

            }

        }

        private void dataGridViewCompList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strBYCompID = this.dataGridViewCompList.Rows[this.dataGridViewCompList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectBYFeeKaiPiaoCateTabID = Convert.ToInt32(strBYCompID, 10);
            cellclick();
        }
        private void cellclick()
        {
            DGBY.Model.BYFeeKaiPiaoCateTab modelBYFeeKaiPiaoCateTab = m_bllBYFeeKaiPiaoCateTab.GetModel(m_nSelectBYFeeKaiPiaoCateTabID);

            if (modelBYFeeKaiPiaoCateTab != null)
            {
                this.textBoxCompName.Text = modelBYFeeKaiPiaoCateTab.BYFeeKaiPiaoCateName;
            }

        }
        private void dataGridViewCompList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewCompList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonBankShowAllRecord_Click(object sender, EventArgs e)
        {
            ShowAllAccountRecordInDataGridView();
        }

        private void buttonBankAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = m_bllBYFeeKaiPiaoCateBankCountTab.GetList("CountNo = '" + this.textBoxAccount.Text + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    MessageBox.Show("银行账号已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.BYFeeKaiPiaoCateBankCountTab modelBYCompBankCountTab = new DGBY.Model.BYFeeKaiPiaoCateBankCountTab();
                modelBYCompBankCountTab.BYFeeKaiPiaoCateTabID = Convert.ToInt32(this.comboBoxCompName.SelectedValue.ToString(), 10);
                modelBYCompBankCountTab.CountName = this.textBoxAccountName.Text;
                modelBYCompBankCountTab.KHBank = this.textBoxBank.Text;
                modelBYCompBankCountTab.CountNo = this.textBoxAccount.Text;

                int nResult = m_bllBYFeeKaiPiaoCateBankCountTab.Add(modelBYCompBankCountTab);


                m_nSelectBYFeeKaiPiaoCateBankCountTabID = m_bllBYFeeKaiPiaoCateBankCountTab.GetMaxId() - 1;
                ShowAllAccountRecordInDataGridView();
                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           

        }

        private void buttonBankModify_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.BYFeeKaiPiaoCateBankCountTab modelBYCompBankCountTab = m_bllBYFeeKaiPiaoCateBankCountTab.GetModel(m_nSelectBYFeeKaiPiaoCateBankCountTabID);
                if (modelBYCompBankCountTab == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                modelBYCompBankCountTab.BYFeeKaiPiaoCateTabID = Convert.ToInt32(this.comboBoxCompName.SelectedValue.ToString(), 10);
                modelBYCompBankCountTab.CountName = this.textBoxAccountName.Text;
                modelBYCompBankCountTab.KHBank = this.textBoxBank.Text;
                modelBYCompBankCountTab.CountNo = this.textBoxAccount.Text;

                m_bllBYFeeKaiPiaoCateBankCountTab.Update(modelBYCompBankCountTab);

                ShowAllAccountRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonBankDel_Click(object sender, EventArgs e)
        {
            if (m_bllBYFeeKaiPiaoCateBankCountTab.GetModel(m_nSelectBYFeeKaiPiaoCateBankCountTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllBYFeeKaiPiaoCateBankCountTab.Delete(m_nSelectBYFeeKaiPiaoCateBankCountTabID);
            ShowAllAccountRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            cellAccountClick();
        }

        private void dataGridViewCompBankList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strBYCompIAccountD = this.dataGridViewCompBankList.Rows[this.dataGridViewCompBankList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectBYFeeKaiPiaoCateBankCountTabID = Convert.ToInt32(strBYCompIAccountD, 10);
            cellAccountClick();
        }
        private void cellAccountClick()
        {
            DGBY.Model.BYFeeKaiPiaoCateBankCountTab modelBYCompBankCountTab = m_bllBYFeeKaiPiaoCateBankCountTab.GetModel(m_nSelectBYFeeKaiPiaoCateBankCountTabID);
            if (modelBYCompBankCountTab != null)
            {
                this.textBoxAccountName.Text = modelBYCompBankCountTab.CountName;
                this.textBoxBank.Text = modelBYCompBankCountTab.KHBank;
                this.textBoxAccount.Text = modelBYCompBankCountTab.CountNo;
                this.comboBoxCompName.SelectedValue = modelBYCompBankCountTab.BYFeeKaiPiaoCateTabID;
            }
            else
            {
                this.textBoxAccountName.Text = "";
                this.textBoxBank.Text = "";
                this.textBoxAccount.Text = "";
            }
        }

        private void dataGridViewCompBankList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewCompBankList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }
    }
}
